import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Evan Metrock
 */
public class AIM {

	/**
	 * Contains user specifed execution options for this instance of AIM
	 */
	private Options options_;
	private Corpus corpus_;
	
	/**
	 * Constructor for AIM class
	 * @param args the command line parameters passed by user
	 * @throws IOException from options parsing
	 * @throws IllegalArgumentException from options parsing
	 * @throws FileNotFoundException from options parsing
	 */
	private AIM(String[] args) throws FileNotFoundException, IllegalArgumentException, IOException  {
		this.options_ = Options.parseArguments(args);
		this.corpus_ = Corpus.createCorpus(this.options_);
	}
	
	/**
	 * Accessor for AIM's 'options' variable
	 * @return AIM's 'option' variable
	 */
	public Options getOptions(){
		return options_;
	}
	
	public Corpus getCorpus(){
		return corpus_;
	}
	
	/**
	 * Main method of execution for AIM.java
	 * @param args params as specified in design and user documentation
	 * @throws IOException this will be thrown in the case that println throws an exception while we are reporting another exception
	 */
	public static void main(String[] args) throws IOException {
		
		//Try to perform text file processing and display appropriate error messages. 
		try{
			//Create Histo instance with command line args
			AIM aim = new AIM(args);
			
			Corpus corpus = aim.getCorpus();
			Options options = aim.getOptions();
			
			//Creates index file AIM.out from the static method createIndex() in IndexGenerator
			IndexGenerator.createIndex(corpus, options);
		}
		//Catch various exceptions thrown in other application classes.
		catch(IllegalArgumentException iae)
		{
			System.err.println(iae.getMessage());
			System.exit(-1);
		}
		catch(FileNotFoundException fnfe)
		{
			System.err.println(fnfe.getMessage());
			System.exit(-1);
		}
		catch(IOException ioe)
		{
			System.err.println(ioe.getMessage());
			System.exit(-1);
		}
		//catch(Throwable t)
		//{
		//	System.err.println("ERROR: program terminated unexpectedly.");
		//	System.exit(-1);
		//}
		
	}	
}
